package com.face.controller;

import com.face.common.Page;
import com.face.common.Result;
import com.face.common.ResultCode;
import com.face.entity.Remark;
import com.face.entity.User;
import com.face.exception.DatabaseException;
import com.face.exception.InsufficientPrivilegesException;
import com.face.exception.ServiceException;
import com.face.service.AccountService;
import com.face.service.RemarkService;
import com.face.service.UserService;
import com.face.util.StringConstans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wechat")
public class WechatCustomerControler {

    @Autowired
    UserService userService;

    @Autowired
    AccountService accountService;

    @Autowired
    RemarkService remarkService;

    /**
     * 获取顾客详情页信息的接口
     *
     * @param userId
     * @param openid
     * @return
     */
    @GetMapping("/detail/{userId}")
    public Result searchCustomerForDetail(@PathVariable String userId, @CookieValue(StringConstans.WECHAT_OPENID) String openid) {

        try {
            Map<String, Object> model = userService.findDetailByUserId(openid, userId);
            return Result.success(model);
        } catch (ServiceException e) {
            return Result.failure(ResultCode.NO_CUSTOMER_RECORD);
        } catch (InsufficientPrivilegesException e) {
            return Result.failure(ResultCode.PERMISSION_DENIED);
        }
    }

    /**
     * 微信端添加到访记录的接口
     *
     * @param remark
     * @param openid
     * @return
     */
    @PostMapping("/note/add")
    public Result addRemark(@RequestBody Remark remark, @CookieValue(StringConstans.WECHAT_OPENID) String openid) {

        try {
            if (remark == null || remark.getDetail().equals("")) {
                return Result.failure(ResultCode.BAD_PARAMETER);
            }
            remarkService.addRemarkByOpenid(remark, openid);
            return Result.success(ResultCode.CREATED);
        } catch (DatabaseException e) {
            return Result.failure(ResultCode.CREATE_FAIL);
        } catch (ServiceException e) {
            return Result.failure(ResultCode.NO_CUSTOMER_RECORD);
        } catch (InsufficientPrivilegesException e) {
            return Result.failure(ResultCode.PERMISSION_DENIED);
        }
    }

    /**
     * 获取顾客最近到访时间与客户信息的接口
     *
     * @param userId
     * @param openid
     * @return
     */
    @GetMapping("/note/get/{userId}")
    public Result searchCustomerForRemark(@PathVariable String userId, @CookieValue(StringConstans.WECHAT_OPENID) String openid) {

        try {
            Map<String, Object> model = userService.findByUserIdOnWechat(openid, userId);
            return Result.success(model);
        } catch (ServiceException e) {
            return Result.failure(ResultCode.NO_CUSTOMER_RECORD);
        } catch (InsufficientPrivilegesException e) {
            return Result.failure(ResultCode.PERMISSION_DENIED);
        }
    }

    /**
     * 搜索顾客到访记录功能的接口
     *
     * @param openid
     * @param start
     * @param end
     * @param keyword
     * @param userId
     * @return
     */
    @GetMapping("/wxSearch/{userId}")
    public Result searchCustomerForRemarks(@CookieValue(StringConstans.WECHAT_OPENID) String openid, @RequestParam(value = "start") String start, @RequestParam(value = "end") String end, @RequestParam(value = "keyword") String keyword, @PathVariable String userId) {

        try {
            List<Map<String, String>> list = remarkService.getRemarksForWx(openid, userId, start, end, keyword);
            return Result.success(list);
        } catch (ServiceException e) {
            return Result.failure(ResultCode.NO_CUSTOMER_RECORD);
        } catch (InsufficientPrivilegesException e) {
            return Result.failure(ResultCode.PERMISSION_DENIED);
        }
    }

    /**
     * 获取顾客列表最近顾客到访记录的接口
     *
     * @param openid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/visited")
    public Result getRecent(@CookieValue(StringConstans.WECHAT_OPENID) String openid, @RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        Page<User> result = userService.findUserListByOpenid(openid, page, limit);
        return Result.success(result);
    }

    /**
     * 解绑当前微信的接口
     *
     * @param openid
     * @return
     */
    @GetMapping("/unband")
    public Result unband(@CookieValue(StringConstans.WECHAT_OPENID) String openid) {
        String username = accountService.findUsernameByOpenid(openid);
        accountService.updateAccountWeixin(username, null);
        return Result.success();
    }
}
